package com.ryan.encrypttool.business.auth.rabbitmq;

import java.io.IOException;
import java.nio.charset.StandardCharsets;

import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import com.rabbitmq.client.Channel;

import lombok.extern.slf4j.Slf4j;

/**
 * @program encrypt-tool
 * @description
 * @author ryan
 * @create 2024-03-12 22:37
 **/
@Component
@Slf4j(topic = "rabbitmq")
public class RabbitmqConsumer {

    // @RabbitListener(queues = RabbitmqCustomConfig.QUEUE_NAME)
    @RabbitListener(bindings = {@QueueBinding(value = @Queue(value = RabbitmqCustomConfig.QUEUE_NAME),
        exchange = @Exchange(value = RabbitmqCustomConfig.EXCHANGE_NAME),key = "ROUTING_KEY")})
    public void receive(String msg, Message message, Channel channel) {

        byte[] body = message.getBody();
        String receivedText = new String(body, StandardCharsets.UTF_8);
        // 现在receivedText已经是UTF-8解码后的字符串了
        log.debug("Received UTF-8 message: {}", receivedText);
        // ...进一步处理消息...
        try {
            channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
        } catch (IOException e) {
            log.error("消息确认异常", e);
            throw new RuntimeException(e);
        }
    }

}
